<%--
 * 软件著作权：ExtJs4 标签所有参与开发者
 --%>
<%@tag import="java.io.StringReader,java.io.BufferedReader,org.apache.commons.lang.StringUtils"%>
<%@tag import="org.apache.commons.beanutils.BeanUtils" pageEncoding="UTF-8" description=""%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ attribute name="adapter" type="java.lang.String" required="false" description="装配器，三种选择：jquery，prototype，yui"%>
<%@ attribute name="debug" type="java.lang.Boolean" required="false" description="是否开启debug模式"%>
<%@ attribute name="extLocation" type="java.lang.String" required="false" description="ext脚本的目录"%>
<%@ attribute name="theme" type="java.lang.String" required="false" description="主题"%>
<%@ attribute name="locale" type="java.lang.String" required="false" description="语言地区"%>
<%@ attribute name="msgTarget" type="java.lang.String" required="false" description="表单信息提示"%>
<%@ attribute name="smProvider" type="java.lang.String" required="false" description=""%>
<%@ attribute name="loadingMask" type="java.lang.Boolean" required="false" description="是否启用加载提示"%>
<%@ attribute name="loadingTime" type="java.lang.Integer" required="false" description="加载提示的时间，默认1秒"%>
<%@ attribute name="items" type="java.lang.String" required="false" description="(String) Items container, generated automatically."%>
<%@ attribute name="message" type="java.lang.Object" required="false" description="消息对象，可以是异常，也可以是String"%>
<%@ attribute name="messageFn" type="java.lang.String" required="false" description="消息对象点击后的事件"%>
<%@ attribute name="messageType" type="java.lang.String" required="false" description="可以是slideIn或者alert,默认是弹出slideIn窗口"%>
<%@ attribute name="loadJs" type="java.lang.String" required="false" description="需要加载的其他js文件，多个用,号分隔"%>
<%@ attribute name="tipUrl" type="java.lang.String" required="false" description="页面信息提示的url"%>
<%@ attribute name="tipWidth" type="java.lang.String" required="false" description="页面信息提示的窗口宽度"%>
<%@ attribute name="tipHeight" type="java.lang.String" required="false" description="页面信息提示的窗口高度"%>
<%@ attribute name="tipShow" type="java.lang.Boolean" required="false" description="页面信息提示的窗口是否默认显示"%>

<c:if test="${empty(tipWidth)}">
	<c:set var="tipWidth" value="400px" />
</c:if>
<c:if test="${empty(tipHeight)}">
	<c:set var="tipHeight" value="250px" />
</c:if>
<c:if test="${empty(tipShow)}">
	<c:set var="tipShow" value="false" />
</c:if>
<%
	if (messageType==null || messageType.equals("")) {
		this.messageType = "slideIn";//通过this.messageType访问得到
		jspContext.setAttribute("messageType","slideIn");//通过${}访问得到
	}
	if (extLocation==null || extLocation.equals("")) {
		this.extLocation = "js/extjs";
		jspContext.setAttribute("extLocation","js/extjs");
	}
	if (locale==null || locale.equals("")) {
		this.locale = "zh_CN";
		jspContext.setAttribute("locale","zh_CN");
	}
	if (loadingTime==null) {
		this.loadingTime = 1000;
		jspContext.setAttribute("loadingTime",1000);
	}
	if (loadingMask==null) {
		this.loadingMask = true;
		jspContext.setAttribute("loadingMask",true);
	}
	if (message==null) {
		this.message = request.getAttribute("message");
	}
	%>

<c:if test="${empty(theme)}">
	<c:set var="theme" value="default" />
</c:if>
	<link id="theme_stylesheet" rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/${extLocation}/resources/css/ext-${theme }.css" />
<c:if test="${adapter == 'jquery'}">
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/jquery/jquery.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/jquery/jquery-plugins.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/jquery/ext-jquery-adapter.js"></script>
</c:if>

<c:if test="${adapter == 'prototype'}">
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/prototype/prototype.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/prototype/scriptaculous.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/prototype/effects.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/prototype/ext-prototype.adapter.js"></script>
</c:if>

<c:if test="${adapter == 'yui'}">
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/yui/yui-utilities.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/adapter/yui/ext-yui-adapter.js"></script>
</c:if>

<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/ext-all${debug?"-debug":""}.js"></script>

<c:if test="${!empty(locale)}">
	<script type="text/javascript" src="<%=request.getContextPath() %>/${extLocation}/locale/ext-lang-${locale}.js"></script>
</c:if>

<script>
	</script>

<c:if test="${!empty(loadJs)}">
	<% String[] jss = StringUtils.split(loadJs,",");
			for (String js : jss) {
		%>
	<script type="text/javascript" src="<%=js %>"></script>
	<% } %>
</c:if>
<c:set var="ext_component_variable" scope="request" />
<c:set var="ext_component_executions" scope="request" />
<c:set var="ext_component_executions_after" scope="request" />
<script>
		Ext.QuickTips.init();
		<c:if test="${!empty(msgTarget)}">
			Ext.form.Field.prototype.msgTarget = '${msgTarget}';
		</c:if>
		//Ext.state.Manager.setProvider(new ${empty(smProvider)?"Ext.state.CookieProvider()":smProvider});
	</script>
<jsp:doBody var="pageHTML" />
<%
		String pageHTML = (String)jspContext.getAttribute("pageHTML");
		String stripPageHtml = "";
		String pageLine = "";
		

		BufferedReader br = new BufferedReader(new StringReader(pageHTML));
        while((pageLine=br.readLine())!=null) {
            if (pageLine.trim().length()>0) stripPageHtml+=pageLine+"\n";
        }
		
		jspContext.setAttribute("pageHTML",stripPageHtml);
	%>
<%-- 
	<script>
		Ext.onReady(function(){
			${ext_component_commons}
		});
	</script>
	--%>
${pageHTML}

<script language="javascript">
		${ext_component_variable};
		Ext.onReady(function(){
				${ext_component_commons};		
				<!-- 定义全局代码语句调用 -->
				${ext_component_executions};
			<c:if test='<%= BeanUtils.getProperty(this,"items")!=null %>'>
				<%= BeanUtils.getProperty(this,"items") %>
			</c:if>			
			<!-- 组件代码完成后需要执行的代码 -->
			${ext_component_executions_after};
		});
	</script>

<c:if test="${loadingMask}">
	<style>
#loading-mask {
	background-color: white;
	height: 100%;
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	z-index: 20000;
}

#loading {
	height: auto;
	position: absolute;
	left: 45%;
	top: 40%;
	padding: 2px;
	z-index: 20001;
}

#loading a {
	color: #225588;
}

#loading .loading-indicator {
	background: white;
	color: #444;
	font: bold 13px Helvetica, Arial, sans-serif;
	height: auto;
	margin: 0;
	padding: 10px;
}

#loading-msg {
	font-size: 10px;
	font-weight: normal;
}
</style>

   <div id="loading-mask" style=""></div>
    <div id="loading">
        <div class="loading-indicator">
            <img src="${extLocation }/resources/themes/images/${theme}/shared/blue-loading.gif" width="32" height="32" style="margin-right:8px;float:left;vertical-align:top;"/>页面载入中...请耐心等待！<br/><span id="loading-msg"> &nbsp; &nbsp; &nbsp; &nbsp; ExtJs4标签开发组</span>
        </div>
    </div>

	<script>
			Ext.onReady(function(){		
				setTimeout(function(){
			        Ext.get('loading').remove();
			        Ext.get('loading-mask').fadeOut({remove:true});
			    }, ${loadingTime});
				<c:if test="${!empty(tipUrl)}">
				PtxPageTip.init({url:'${tipUrl}',width:'${tipWidth}',height:'${tipHeight}',showFirst:${tipShow}});
				</c:if>
			});
		</script>
</c:if>

<script>
			Ext.onReady(function(){
				//根据cookie判断个人选择主题
				var theme = getCookie("ExtTheme");
				if (theme) {
					document.getElementById("theme_stylesheet").href="<%=request.getContextPath() %>/${extLocation}/resources/css/xtheme-"+theme+".css";
				}
			});
		</script>

<c:if test="${!empty(message)}">
	<script type="text/javascript">	
		<%
		if (debug && message instanceof Throwable) {
			Throwable exception = (Throwable)message;
		%>
			Ext.onReady(function() {
				new Ext.Window({ 
			    title: '<div style="color:red;font-weight:bold"><%=exception.getMessage()%></div>', 
			    xtype:'window', 
			    modal: true, 
			    width:500, 
			    height:300,
			    closable : true,
			    maximizable : true,
				layout:"border",
				items:[
					{region:"center",titleCollapse:true, autoScroll:true, collapsible:true, title:"<div style='font-style:italic'>错误明细</div>", collapsed:true, 
						html:'<p><%=org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(exception).replaceAll("\n", "<br/>").replace("\r", "<br/>")%></p>'}
					]
				}).show();
			});		
		<%
		} else {
			if (message instanceof Throwable) {
				this.message = ((Throwable)message).getMessage();
				jspContext.setAttribute("message",this.message);
				%>
				Ext.Msg.show({
					closable : false,
					title:'错误提示',
					maxWidth: 300,
					msg: '${message}',
					modal: true, 
					buttons: Ext.Msg.OK,
					<c:if test="${!empty(messageFn)}">
					fn : ${messageFn }.createCallback('${message}'),
					</c:if>				
					icon: Ext.MessageBox.ERROR
				});
				<%
			} else {
				%>
				Ext.onReady(function() {
					<%
						if (messageType==null || !messageType.equals("slideIn")) {
					%>
						Ext.Msg.show({
							closable : false,
							title:'操作提示',
							maxWidth: 300,
							msg: '${message}',
							modal: true, 
							buttons: Ext.Msg.OK,
							<c:if test="${!empty(messageFn)}">
							fn : ${messageFn }.createCallback('${message}'),
							</c:if>				
							icon: Ext.MessageBox.INFO
						});
					<% } else {%>
						Ext.ptx.SlideMsg.msg('操作提示','${message}');
						<c:if test="${!empty(messageFn)}">
							setTimeout(${messageFn }.createCallback('${message}'),3000);
						</c:if>	
					<% } %>
				});
				<%				
			}
		}
		%>
		</script>
</c:if>
<script>
		<c:if test="${!empty(param.message)}">
		<c:if test="${param.messageType=='alert'}">
			Ext.Msg.show({
				closable : false,
				title:'操作提示',
				maxWidth: 300,
				msg: decodeURIComponent('${param.message}'),
				modal: true, 
				buttons: Ext.Msg.OK,
				<c:if test="${!empty(messageFn)}">
				fn : ${messageFn }.createCallback('${param.message}'),
				</c:if>				
				icon: Ext.MessageBox.INFO
			});	
		</c:if>
		<c:if test="${param.messageType=='slideIn'}">
			Ext.ptx.SlideMsg.msg('操作提示','${param.message}');
			<c:if test="${!empty(messageFn)}">
				setTimeout(${messageFn }.createCallback('${param.message}'),3000);
			</c:if>				
		</c:if>
		</c:if>
		</script>
